#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
const int N = 1e6 + 5;

bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    vector<int> b(n + 2);
    for (int i = 1; i <= n; i++) {
        cin >> b[i];
    }
    vector<pair<int, int>> a(n + 2);
    for (int i = 1; i <= n; i++) {
        a[i] = {b[i], i};
    }
    a[n + 1] = {INT_MAX, 0};
    sort(a.begin() + 1, a.begin() + n + 1);
    int l = a[1].second;
    int r = a[1].second;
    int max_val = max(a[1].first * 2, a[n].first);
    int res = max_val - min(a[1].first * 2, a[2].first);
    for (int i = 2; i <= n; i++) {
        while (a[i].second < l) {
            l--;
            max_val = max(max_val, b[l] * 2);
        }
        while (a[i].second > r) {
            r++;
            max_val = max(max_val, b[r] * 2);
        }
        res = min(res, max_val - min(a[1].first * 2, a[i + 1].first));
    }
    cout << res << endl;

    return 0;
}